package SQLite;

import android.content.Context;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;

public class SQLFunction {

    static DBHelper helper;

    public static void initTable(Context context) {
        helper = new DBHelper(context);
        helper.getReadableDatabase();
    }

    /**
     * 【插入数据】
     **/
    public static void insert(Context context, Object[] data) {
        Log.i("TAG:", "插入数据到数据库表：person中:" + data.toString());
        DBManager sqlManager = new DBManager(context);
        helper = new DBHelper(context);
        helper.getWritableDatabase();
        String sql = "insert into person (userid, name , info,phoneNum) values (? , ? , ?, ?)";
        Object[] bindArgs = data;
        sqlManager.updateSQLite(sql, bindArgs);
    }

    public static void deletes(Context context, Object[] data) {
        DBManager sqlManager = new DBManager(context);
        String sql1 = "delete from TableName;";
        String where1 = " 1=1 ";
        String where2 = " 1=1 ";
        String where3 = " 1=1 ";
        sqlManager.updateSQLite(sql1, new String[]{where1, where2, where3});
    }

    /**
     * 【模糊查询】
     **/
    public static ArrayList<HashMap<String, String>> query(Context context, String where1, String where2, String where3) {
        DBManager sqlManager = new DBManager(context);
//        String sql1 = "delete from person";
//        sqlManager.updateSQLite(sql1, null);
        where1 = " 1=1 ";
        where2 = " 1=1 ";
        where3 = " 1=1 ";
        ArrayList<HashMap<String, String>> list1 = new ArrayList<>();
        String sql1 = "select * from person where 1=1 and ? and ? and ? order by _id desc;";
        list1 = sqlManager.querySQLite(sql1, new String[]{where1, where2, where3});
//        String sql = "select * from person where 1=1 and ? and ? and ? order by _id asc LIMIT 2 OFFSET "+(list1.size()-1)+";";
//        list = sqlManager.querySQLite(sql, new String[]{where1, where2, where3});
//        String sql = "select * from person where 1=1 and ? and ? and ? and _id=(select (max(_id))) from person);";
//        if (where1 == null) {
//            list = sqlManager.querySQLite(sql, new String[]{"%", "%", "%"});
//        } else {
//            where1 = " 1=1 ";
//            where2 = " 1=1 ";
//            where3 = " 1=1 ";
//            list = sqlManager.querySQLite(sql, new String[]{where1, where2, where3});
//        }
        Log.i("TAG:", "查询完毕，返回数据：" + list1.size());
        return list1;
    }

    /**
     * 【模糊查询】
     **/
    public static ArrayList<HashMap<String, String>> querys(Context context, String where1, String where2, String where3) {
        DBManager sqlManager = new DBManager(context);
//        String sql1 = "delete from person";
//        sqlManager.updateSQLite(sql1, null);
        where1 = where1;
        where2 = " 1=1 ";
        where3 = " 1=1 ";
        ArrayList<HashMap<String, String>> list1 = new ArrayList<>();
        String sql1 = "select * from person where 1=1 and userid=? and ? and ? order by _id desc;";
        list1 = sqlManager.querySQLite(sql1, new String[]{where1, where2, where3});
//        String sql = "select * from person where 1=1 and ? and ? and ? order by _id asc LIMIT 2 OFFSET "+(list1.size()-1)+";";
//        list = sqlManager.querySQLite(sql, new String[]{where1, where2, where3});
//        String sql = "select * from person where 1=1 and ? and ? and ? and _id=(select (max(_id))) from person);";
//        if (where1 == null) {
//            list = sqlManager.querySQLite(sql, new String[]{"%", "%", "%"});
//        } else {
//            where1 = " 1=1 ";
//            where2 = " 1=1 ";
//            where3 = " 1=1 ";
//            list = sqlManager.querySQLite(sql, new String[]{where1, where2, where3});
//        }
        Log.i("TAG:", "查询完毕，返回数据：" + list1.size());
        return list1;
    }

    /**
     * 【删除数据】
     **/
    public static void delete(Context context, Object[] data) {
        DBManager sqlManager = new DBManager(context);
        String sql1 = "delete from person where _id<11;";
        String where1 = " 1=1 ";
        String where2 = " 1=1 ";
        String where3 = " 1=1 ";
        sqlManager.updateSQLite(sql1, new String[]{where1, where2, where3});
    }

    /**
     * 【更新数据】
     **/
    public static void update(Context context, Object[] data) {
        helper = new DBHelper(context);
        helper.getReadableDatabase();
        DBManager sqlManager = new DBManager(context);
        String sql = "update person set name=? , info=? where _id=?";
        sqlManager.updateSQLite(sql, data);
    }
}